package com.top.mall.main.access

import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import com.top.mall.R
import com.top.mall.custom.setTransitionStatus
import com.top.mall.jetpack.vm.AccessViewModel
import com.top.mall.main.home.MainActivity
import kotlinx.android.synthetic.main.fragment_access_login_pwd.*

class AccessLoginPwdFragment : OwnerViewModeFragment<AccessViewModel>() {

    companion object {
        fun newInstance(): AccessLoginPwdFragment {
            return AccessLoginPwdFragment()
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_login_pwd

    override fun viewModel(): AccessViewModel {
        return createViewModel()
    }

    override fun initView() {
        text_login_pwd_login.setOnClickListener {
            viewModel.accessLogin(
                edit_login_pwd_phone.text.toString(),
                edit_login_pwd_pwd.text.toString()
            )
        }
    }

    override fun initData() {
        viewModel.observableLoginPwd(edit_login_pwd_phone, edit_login_pwd_pwd)
    }

    override fun initViewMode() {
        viewModel.isEnabledLoginPwdLiveData.observe(viewLifecycleOwner, Observer { isEnabled ->
            isEnabled?.let {
                text_login_pwd_login.isEnabled = it
            }
        })

        viewModel.accessLoginLiveData.observe(viewLifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            MainActivity.open()
            requireActivity().finish()
        })
    }

}